#include <Multi_Timer_V2.h>

/* Demonstrate Multi_Timer_V2 flasher timer function

Operation and expected result:
- Connect an LED to pin D10 with appropriate current limiting
resistor R1.

       LED     R1
 D10 --->|---/\/\/--- GND

Connect input D4 to GND with either a switch or breadboard jumper.
Do the same for input D2.

- Start the IDE serial monitor. Insure baud rates between
processor and monitor match.

When D4 (switch1) is grounded the timer is enabled and will run to
preset and then auto-reset.  The external LED is turned on for
timerOnTime milliseconds each interval and a count of intervals is
displayed on the serial monitor.

Pressing D2 (option switch) will load a new on time to the flasher object and
the LED will reflect the new on time.

*/

const unsigned long timerInterval = 2000;
const unsigned long timerOnTime = 1700;

// Instantiate a Multi_Timer_V2 object
FlasherTimer myTimer1(timerInterval, timerOnTime);

int counter1 = 0;

byte externalLED = 10;
byte switch1 = 4;
byte optionSwitch = 2;

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(externalLED, OUTPUT);
  pinMode(switch1, INPUT_PULLUP);
  pinMode(optionSwitch, INPUT_PULLUP);
}

void loop() {
  myTimer1.update();  // refresh the timer value and flags

  // Enable/disable the timer using the ternary operator
  myTimer1.setEnable(digitalRead(switch1) == HIGH ? false : true);

  // Change the on time of the flasher
  if (digitalRead(optionSwitch) == LOW) {
    myTimer1.setOnTime(300);
  }

  //  When the timer resets:
  if (myTimer1.getDoneRose()) {
    Serial.println(++counter1);
  }
  // This is the flasher output
  digitalWrite(externalLED, myTimer1.isFlashing());
}
